﻿using System;
using System.IO;
using CodeGeneration.Model;
using NVelocity;

namespace CodeGeneration.Tools
{
    partial class GenerateCode
    {
        private void CreateInterface(CreateConfig config, VelocityContext context)
        {
            Template template = BuildTemplate("service.vm", config);

            string filePath = config.InterfacePath + config.InterfaceFileName + ".cs";
            //不覆盖同名文件
            if (File.Exists(filePath) && !config.IsOverwriteFile)
            {
                Console.WriteLine("文件：{0}已经存在，跳过生成",filePath);
                return;
            }

            if (!Directory.Exists(config.InterfacePath))
            {
                Directory.CreateDirectory(config.InterfacePath);
                Console.WriteLine("自动创建目录：{0}", config.InterfacePath);
            }

            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            template.Merge(context, sw);
            sw.Close();
            Console.WriteLine("生成文件：{0}",filePath);
        }

    }
}
